package gugga.utils
{
	import flash.utils.Dictionary;
	
	import gugga.collections.dictionary.Dictionary;
	import gugga.collections.linkedList.LinkedList;
	import gugga.collections.checkList.CheckList;
	
	public class CollectionUtility
	{
		public static function arrayToString(aArray : Array) : String
		{
			var result : String = aArray.join(", ");
			
			return result;
		}
		
		public static function containsValue(aCollection:Object, aValue:Object) : Boolean
		{
			if(aCollection is LinkedList)
			{
				return LinkedList(aCollection).contains(aValue);
			}
			else if(aCollection is flash.utils.Dictionary)
			{
				var dictionary : gugga.collections.dictionary.Dictionary = 
					new gugga.collections.dictionary.Dictionary(true, flash.utils.Dictionary(aCollection));
					
				var containsValue : Boolean = dictionary.containsValue(aValue);
				
				return containsValue;
			}
			else if(aCollection is Array)
			{
				var result : Boolean = false;
				
				var array : Array = aCollection as Array;
				if(array.indexOf(aValue) != -1)
				{
					result = true;
				}
				
				return result;
			}
			else if(aCollection is CheckList)
			{
				return CheckList(aCollection).isObjectRegistered(aValue);
			}
			else
			{
				for (var key:String in aCollection)
				{
					if(aCollection[key] === aValue)
					{
						return true;
					}
				}
				
				return false;
			}
		}
		
		public static function containsKey(aCollection:Object, aKey:Object) : Boolean
		{
			if(aCollection is flash.utils.Dictionary)
			{
				var dictionary : gugga.collections.dictionary.Dictionary = 
					new gugga.collections.dictionary.Dictionary(true, flash.utils.Dictionary(aCollection));
					
				var containsKey : Boolean = dictionary.containsKey(aKey);
				
				return containsKey;
			}
			else
			{
				for (var key:String in aCollection)
				{
					if(key === aKey)
					{
						return true;
					}
				}
				
				return false;
			}
		}
	}
}